package com.echo.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.echo.dao.AssessmentDao;
import com.echo.entity.dto.AssessmentDTO;
import com.echo.entity.pojo.Assessment;
import com.echo.entity.vo.AssessmentVO;
import com.echo.service.AssessmentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

/**
 * @author ATSELI
 * @version 1.0
 * @date 2021/7/18 11:37
 */
@Service
@Transactional(rollbackForClassName = "Exception.class")
public class AssessmentServiceImpl implements AssessmentService {
    private final AssessmentDao assessmentDao;

    public AssessmentServiceImpl(AssessmentDao assessmentDao) {
        this.assessmentDao = assessmentDao;
    }


    @Override
    public int add(AssessmentDTO assessmentDTO, HttpServletRequest request) {
        // 查询是否已经提交过申请
        Assessment checkResult = assessmentDao.checkUser((int)request.getAttribute("userId"));
        if (checkResult != null){
            if (checkResult.getApplicationStatus() == 1){
                // 申请已提交
                return -1;
            }
            if (checkResult.getApplicationStatus() == 3){
                // 已申请成功
                return -2;
            }
        }

        Assessment assessment = new Assessment();
        BeanUtil.copyProperties(assessmentDTO, assessment);
        // assessment.setUserId((int)request.getAttribute("userid"));
        assessment.setUserId(1);
        // assessment.setUsername(request.getAttribute("username").toString());
        assessment.setUsername("atseli");
        assessment.setApplicationStatus(0);
        return assessmentDao.add(assessment);
    }

    @Override
    public AssessmentVO select(HttpServletRequest request) {
        // Assessment checkResult = assessmentDao.checkUser((int)request.getAttribute("userId"));
        Assessment checkResult = assessmentDao.checkUser(1);
        System.out.println("============>"+checkResult);
        AssessmentVO assessment = new AssessmentVO();
        BeanUtil.copyProperties(checkResult, assessment);
        if (checkResult.getApplicationStatus() == 0){
            assessment.setStatus("申请中");
        }else if (checkResult.getApplicationStatus() == 1){
            assessment.setStatus("申请失败");
        }else if (checkResult.getApplicationStatus() == 2){
            assessment.setStatus("申请成功");
        }
        return assessment;
    }
}
